package java6699.lesson07;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import java.io.File;
import java.io.IOException;

/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-10-18 09:04
 **/
public class CreateNewFile6699Controller {
    @FXML
    public TextField tfFilename;
    @FXML
    public Label lblMessage;
    @FXML
    public void listAll(ActionEvent actionEvent) throws IOException {
        String filename = tfFilename.getText().trim();
        File file = new File(filename);
        if(!file.exists()){
            //通过此段代码确保存放文件的路径存在,防止直接通过f进行判断会创建出文件，后续会出现同名现象
            File parent = file.getParentFile();
            if(parent !=null && !parent.exists()){
                if (!parent.mkdirs()){
                    throw new IOException("不能创建目录：" + parent);
                }
                lblMessage.setText("创建目录：" + parent);
            }
            file.createNewFile();
            lblMessage.setText("创建新文件：" + file.getAbsolutePath());
        }else {
            lblMessage.setText("文件已存在");
        }
    }

}